package com.xcoder.leetcode;

/**
 * https://leetcode-cn.com/problems/can-place-flowers/
 * <p>
 * 思路：贪心算法
 */
public class _605_种花问题 {

    public static boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (n == 0) {
            return true;
        }
        int count = 0;
        int i = 0;
        while (i < flowerbed.length) {
            if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) &&
                    (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
                flowerbed[i] = 1;
                count++;
                if (count >= n) {
                    return true;
                }
            }
            i++;
        }
        return false;
    }
}
